/** 
 * Attack.c - The C source file of the task
 * NOTE: This file is based on C99 and rt-thread
 *      该文件实现的任务有：
 *      - 电磁炮状态管理
 *      - 攻击服务
 *
 * Copyright (c) 2020-, magnetic Emit Project
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes           mail
 * 2021-06-16     江榕煜       first version   2373180028@qq.com
**/

#include "system.h"

#define _ATTACK_DEBUG_

ALIGN(RT_ALIGN_SIZE)
static struct rt_thread thread_Attack;  // 线程静态内存
static char stack_Attack[512];

static struct rt_mailbox mb_AttackCmd;
static char mb_Attack_Pool[16];

/*----------------------具体实现---------------------*/

/** @brief  攻击控制服务
 *  @note   使用邮箱触发控制
 *  @author 江榕煜（2021.6.16）
 *  @param 
 *  @retval
**/
static void thread_Attack_Entry(void* entryParam)
{
    rt_uint32_t cmdNow;
    for(;;)
    {
        if(rt_mb_recv(&mb_AttackCmd,&cmdNow,RT_WAITING_FOREVER) == RT_EOK)
        {
            switch(cmdNow)
            {
                case(0):
                    #ifdef _ATTACK_DEBUG_
                    rt_kprintf("---电磁炮待命!---\n");
                    #endif
                    magneticEmitCtrl(standby);
                    break;
                case(1):
                    #ifdef _ATTACK_DEBUG_
                    rt_kprintf("---电磁炮充电!---\n");
                    #endif
                    magneticEmitCtrl(charge);
                    break;
                case(2):
                    #ifdef _ATTACK_DEBUG_
                    rt_kprintf("---电磁炮发射!---\n");
                    #endif
                    magneticEmitCtrl(EMIT);
                    break;
                    
            }
        }
    }
}

/** @brief  攻击控制服务初始化
 *  @note   
 *  @author 江榕煜（2021.6.16）
 *  @param  None
 *  @retval
 *          sem_Attack_t(rt_sem_t)  信号量对象
**/
rt_mailbox_t thread_Attack_Init(void)
{
    /* 邮箱对象初始化 */
    rt_err_t res;
    res = rt_mb_init(&mb_AttackCmd,"attackCmd",
                        &mb_Attack_Pool[0],
                        sizeof(mb_Attack_Pool)/4,
                        RT_IPC_FLAG_FIFO);
    if(res != RT_EOK)
    {
        rt_kprintf("攻击服务邮箱对象初始化失败!\n");
        return RT_NULL;
    }
    
                        
    /* 静态线程初始化 */
    rt_thread_init(&thread_Attack,
                    "Attack thread",
                    thread_Attack_Entry,
                    RT_NULL,
                    &stack_Attack[0],
                    sizeof(stack_Attack),
                    2,20);
                    
    
    rt_thread_startup(&thread_Attack);  //启动线程
    rt_kprintf("Attack-thread start success!\n");
                    
    return &mb_AttackCmd;
}

#ifdef _ATTACK_DEBUG_

void attack(int argn,char ** argv)
{
    if(argn < 2)
    {
        rt_kprintf("参数错误 \n");
        return;
    }
    
    rt_uint32_t cmd;
    switch(argv[1][0])
    {
        case('0'):
            cmd = 0;
            break;
        case('1'):
            cmd = 1;
            break;
        case('2'):
            cmd = 2;
            break;
    }
    rt_mb_send(&mb_AttackCmd,cmd);
}
MSH_CMD_EXPORT(attack,攻击类指令);
#endif
